package com.springboot.controller.blog;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.config.controller.BaseController;
import com.springboot.file.BaseFileStoreFileUtil;
import com.springboot.file.filedto.MultipartFileDto;
import com.springboot.jwt.interfaces.JwtToken;
import com.springboot.returndata.dto.blog.BlogArticlePageDto;
import com.springboot.returndata.dto.blog.BlogArticleUpAdDto;
import com.springboot.returndata.dto.blog.BlogMeInfoDto;
import com.springboot.returndata.dto.blog.UpdateBlogArticleStatusDto;
import com.springboot.returndata.vo.blog.BlogArticleInfoVo;
import com.springboot.returndata.vo.blog.BlogArticleVo;
import com.springboot.returndata.vo.blog.BlogMeInfoVo;
import com.springboot.service.blog.BlogArticleService;
import com.springboot.service.blog.BlogArticleTagService;
import com.springboot.service.blog.BlogSortService;
import com.springboot.util.enums.RespErrorCode;
import com.springboot.util.http.ResponseBuilder;
import com.springboot.util.http.ResultResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.net.URLEncoder;
import java.util.Map;

/**
 * <p>
 * 博客文章内容表 前端控制器
 * </p>
 *
 * @author zyj
 * @since 2023-03-23
 */
@RestController
@RequestMapping("/blogArticle")
@Slf4j
@Tag(name = "博客文章管理")
public class BlogArticleController extends BaseController {

    @Resource
    private BaseFileStoreFileUtil ftpUtils;

    @Resource
    private BlogArticleService blogArticleService;

    @Resource
    private BlogArticleTagService blogArticleTagService;

    @Resource
    private BaseFileStoreFileUtil baseFileStoreFileUtil;


    @PostMapping(value = "/pageBlogArticle")
    @Operation(description = "获取文章分类列表")
    public ResultResponse<Page<BlogArticleVo>> pageBlogArticle(@RequestBody BlogArticlePageDto blogArticlePageDto){
        try {
            blogArticlePageDto.setPreViewImg(baseFileStoreFileUtil.getDomain());
            Page<BlogArticleVo> blogSort = blogArticleService.pageBlogArticle(blogArticlePageDto);
            return ResponseBuilder.successData(blogSort);
        }catch (Exception e){
            log.error("pageBlogSort error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.GET_INFO_NULL.getValue(), RespErrorCode.GET_INFO_NULL.getDescripe());

    }

    @PostMapping(value = "/updateBlogArticleStatus")
    @Operation(description = "修改文章状态")
    public ResultResponse<String> updateBlogArticleStatus(@RequestBody UpdateBlogArticleStatusDto updateBlogArticleStatusDto){
        try {
            blogArticleService.updateBlogArticleStatus(updateBlogArticleStatusDto);
            return ResponseBuilder.success();
        }catch (Exception e){
            log.error("updateBlogArticleStatus error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.UPDATE_ERROR.getValue(), RespErrorCode.UPDATE_ERROR.getDescripe());

    }

    @PostMapping(value = "/addBlogArticle")
    @Operation(description = "保存文章信息")
    public ResultResponse<String> addBlogArticle(@RequestBody BlogArticleUpAdDto blogArticleUpAdDto ){
        try {
            blogArticleService.addBlogArticle(blogArticleUpAdDto);
            return ResponseBuilder.success();
        }catch (Exception e){
            log.error("pageBlogSort error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.SAVE_ERROR.getValue(), RespErrorCode.SAVE_ERROR.getDescripe());

    }


    @PostMapping(value = "/updateBlogArticle")
    @Operation(description = "更新文章信息")
    public ResultResponse<String> updateBlogArticle(@RequestBody BlogArticleUpAdDto blogArticleUpAdDto ){
        try {
            blogArticleService.updateBlogArticle(blogArticleUpAdDto);
            return ResponseBuilder.success();
        }catch (Exception e){
            log.error("updateBlogArticle error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.SAVE_ERROR.getValue(), RespErrorCode.SAVE_ERROR.getDescripe());

    }

    @GetMapping(value = "/getBlogArticleInfo")
    @Operation(description = "获取文章信息")
    public ResultResponse<BlogArticleInfoVo> getBlogArticleInfo(Integer id){
        try {
            BlogArticleInfoVo blogArticleInfo = blogArticleService.getBlogArticleInfo(id);
            return ResponseBuilder.successData(blogArticleInfo);
        }catch (Exception e){
            log.error("getBlogArticleInfo error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.GET_INFO_NULL.getValue(), RespErrorCode.GET_INFO_NULL.getDescripe());

    }


    @DeleteMapping("/deleteBlogArticle")
    @Operation(description = "根据id删除文章")
    public ResultResponse<Object> deleteBlogArticle(@RequestBody Map<String, Integer> param){
        try {
            blogArticleService.deleteBlogArticle(param.get("id"));
            return ResponseBuilder.success();
        }catch (Exception e){
            log.error("updateBlogSort error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.DELETE_ERROR.getValue(), RespErrorCode.DELETE_ERROR.getDescripe());

    }


    @JwtToken
    @GetMapping("/getTableImg")
    @Operation(description = "获取列表图片")
    public void getTableImge(@RequestParam String imgUrl){
        try {
            HttpServletResponse response = super.getResponse();
            String fileName = URLEncoder.encode(imgUrl, "UTF-8");
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
            byte[] bytes = ftpUtils.downloadFile(imgUrl);
            FileCopyUtils.copy(bytes,response.getOutputStream());
        }catch ( Exception e){
            log.error("获取列表图片失败", e);
        }
    }


    @PostMapping(value = "/uploadImgBlog")
    public ResultResponse<Object> uploadImgBlog(@RequestPart("file") MultipartFile[] multipartFiles){
        try {
            Map<String, String> imgBlog = blogArticleService.uploadImgBlog(multipartFiles);
            return ResponseBuilder.successData(imgBlog);
        } catch (Exception e) {
            log.error("uploadImgBlog error", e);
        }

        return ResponseBuilder.errorMessage(RespErrorCode.UPLOAD_ERROR.getValue(), RespErrorCode.UPLOAD_ERROR.getDescripe());
    }

    /**
     * 编辑器图片上传
     * @param multipartFiles
     * @return
     */
    @PostMapping(value = "/uploadImg")
    public ResultResponse<Object> uploadImg(@RequestPart("editormd-image-file") MultipartFile[] multipartFiles){
        try {
            MultipartFileDto multipartFileDto = new MultipartFileDto(multipartFiles[0], BlogSortService.blogFilePath);
            String path = ftpUtils.uploadMultipartFile(multipartFileDto);
            String articleImg = ftpUtils.getFileLink(path);
            return ResponseBuilder.successData(articleImg);
        } catch (Exception e) {
            log.error("uploadImg error", e);
        }

        return ResponseBuilder.errorMessage(RespErrorCode.UPLOAD_ERROR.getValue(), RespErrorCode.UPLOAD_ERROR.getDescripe());
    }

    @GetMapping(value = "/getMeContentInfo")
    @Operation(description = "获取关于我内容")
    public ResultResponse<Object> getMeContentInfo() {
        try {
            BlogMeInfoVo meContentInfo = blogArticleTagService.getMeContentInfo();
            return ResponseBuilder.successData(meContentInfo);
        } catch (Exception e) {
            log.error("getMeContentInfo error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.GET_INFO_NULL.getValue(), RespErrorCode.GET_INFO_NULL.getDescripe());

    }

    @PostMapping(value = "/saveMeContentInfo")
    @Operation(description = "保存关于我内容")
    public ResultResponse<Object> saveMeContentInfo(@RequestBody BlogMeInfoDto blogMeInfoDto) {
        try {
            blogArticleTagService.saveMeContentInfo(blogMeInfoDto);
            return ResponseBuilder.success();
        } catch (Exception e) {
            log.error("saveMeContentInfo error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.SAVE_ERROR.getValue(), RespErrorCode.SAVE_ERROR.getDescripe());

    }

}

